
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package user;

/**
 *
 * @author Killian
 */

import java.util.Map;  
import org.apache.struts2.dispatcher.SessionMap;  
import org.apache.struts2.interceptor.SessionAware;  
import com.opensymphony.xwork2.ActionSupport;  
import util.Utils;
  
public class LoginAction extends ActionSupport implements SessionAware{  
    private String identifiant,password;     
    SessionMap<String,Object> sessionmap;  

    public String getIdentifiant() {  
        return identifiant;  
    }  

    public void setIdentifiant(String identifiant) {  
        this.identifiant = identifiant;  
    }  

    public String getPassword() {  
        return password;  
    }  

    public void setPassword(String password) {  
        this.password = password;  
    }  

    public String execute(){ 
        boolean is_correct = true;
        if(identifiant.isEmpty()) {
            addFieldError("identifiant", getText("error.identifiant"));
            is_correct = false;
        }
        if(password.isEmpty()) {
            addFieldError("password", getText("error.password"));
            is_correct = false;
        }
        if(!identifiant.isEmpty() && !password.isEmpty()) {
            //if(LoginDAO.validate(identifiant, Utils.encrypt(password, getText("key")))){       
            if(LoginDAO.validate(identifiant, password)){         
                sessionmap.put("authentifie",true);
                User u = UserAction.getUserByIdentifiant(identifiant);
                sessionmap.put("user",u); 
                System.out.print(u);
                System.out.print(u.getDroit().getType_droit());
                sessionmap.put("droit",u.getDroit().getType_droit());                 
            }  
            else{  
                addFieldError("badInfos",getText("connexion.badInfos"));
                is_correct = false;  
            }
        }
        if(!is_correct) return INPUT;
        else return SUCCESS;  
    }
    
    public String changeLanguage() {
        System.out.println("caca");
        return SUCCESS;
    }

    public void setSession(Map map) {  
        sessionmap=(SessionMap)map;   
    }  

    public String logout(){  
        sessionmap.invalidate();  
        sessionmap.clear();
        return SUCCESS;  
    }  
    
    public String loginRedirect() {
        return SUCCESS;
    }
}  
